package com.control.qct.businesssteward.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.adapter.StoreSelectAdapter;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.control.qct.businesssteward.httputils.NetProtocol;
import com.control.qct.businesssteward.model.StoreModel;
import com.control.qct.businesssteward.utils.EncodeUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StoreSelectActivity extends BaseActivity {
    @BindView(R.id.rv_store_select)
    RecyclerView mRvStoreSelect;
    private StoreSelectAdapter mStoreSelectAdapter;
    private String mStoreId;
    private Map<String, String> mParams;

    public StoreSelectActivity() {
        super(R.layout.activity_store_select);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mStoreId = extras.getString("store_id");
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("门店选择");
        getStore();
        mRvStoreSelect.setLayoutManager(new LinearLayoutManager(this));
        mStoreSelectAdapter = new StoreSelectAdapter();
        mRvStoreSelect.setAdapter(mStoreSelectAdapter);
        mStoreSelectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mStoreSelectAdapter.setPosition(position);
                selectStore(((List<StoreModel>) adapter.getData()).get(position));
            }
        });
    }

    /**
     * 选择门店
     */
    private void selectStore(final StoreModel storeModel) {
        mParams = new HashMap<>();
        mParams.put("companyId", new String(EncodeUtils.base64Decode(mSPUtils.getString("enterprise_code"))));
        mParams.put("loginName", new String(EncodeUtils.base64Decode(mSPUtils.getString("user"))));
        mParams.put("loginPassword", new String(EncodeUtils.base64Decode(mSPUtils.getString("password"))));
        mParams.put("storeId", storeModel.getStoreId());
        mParams.put("token", mNetProtocol.getNowToken());
        mNetProtocol.login(this, mParams, "正在选择门店...", new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                showShortToast("门店选择成功!");
                mSPUtils.put(mNetProtocol.getUserCode() + "_store", storeModel);
                Intent intent = new Intent();
                intent.putExtra("store", storeModel);
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onFail(BaseBean baseBean) {

            }
        });
    }

    /**
     * 获取门店
     */
    private void getStore() {
        mNetProtocol.getStore(this, true, new HttpUtils.IHttpResult() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                List<StoreModel> data = (List<StoreModel>) baseBean.getData();
                mStoreSelectAdapter.addData(data);
                for (int i = 0; i < data.size(); i++) {
                    if (data.get(i).getStoreId().equals(mStoreId)) {
                        mStoreSelectAdapter.setPosition(i);
                        mRvStoreSelect.scrollToPosition(i);
                    }
                }
            }

            @Override
            public void onFail(BaseBean baseBean) {
                showShortToast(baseBean.getMessage());
            }
        });
    }
}
